package ca.camosun.chatbox;

import java.util.ArrayList;

import ca.camosun.chatbox.network.Room;
import ca.camosun.chatbox.network.discovery.BluetoothDiscovery;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

	private static final int REQUEST_ENABLE_BT = 0;
	private BluetoothDiscovery mBluetoothDiscovery;
	private ArrayAdapter<BluetoothDevice> mBluetoothDiscoveredListAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mBluetoothDiscovery = new BluetoothDiscovery(getApplicationContext());
        ListView bluetoothListView = (ListView) findViewById(R.id.bluetoothDiscoveredList);
        mBluetoothDiscoveredListAdapter = new ArrayAdapter<BluetoothDevice>(this, 
        		android.R.layout.simple_list_item_1);
        bluetoothListView.setAdapter(mBluetoothDiscoveredListAdapter);
        
		final Button button = (Button) findViewById(R.id.bluetoothDiscoveryButton);
        button.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
            	mBluetoothDiscoveredListAdapter.clear();
            	mBluetoothDiscovery.discover(new BluetoothDiscovery.OnCompletedDiscovery() {
					public void execute(Room<BluetoothDevice> foundDevices) {
						mBluetoothDiscoveredListAdapter.addAll(foundDevices.people);
					}
				});
            }
        });
		
		Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
	    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	void main(){
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
		    // Probably just kick them out, unless we offer a wifi option.
			throw new UnsupportedOperationException(
					"This device does not support Bluetooth");
		}
		
		if (!mBluetoothAdapter.isEnabled()) {
		    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
		
		Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
		startActivity(discoverableIntent);
		
		final ArrayList<String> devices = new ArrayList<String>();
		
		final BroadcastReceiver mReceiver = new BroadcastReceiver() {
		
		    public void onReceive(Context context, Intent intent) {
		    	
		        String action = intent.getAction();
		        
		        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
		            
		            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
		            
		            devices.add(device.getName() + "," +device.getAddress());
		        }
		    }
		};
		
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		registerReceiver(mReceiver, filter);
		mBluetoothAdapter.cancelDiscovery();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		mBluetoothDiscovery.destroy();
	}
}
